package com.loop.unclelend.ui.presenter

import com.loop.unclelend.app_config.UncleConfigs
import com.loop.unclelend.network.ApiService
import com.loop.unclelend.network.IHttpCallback
import com.loop.unclelend.pro.ProPresenter
import com.loop.unclelend.ui.view.IView_Steps
import com.loop.unclelend.utils.helper.CustomProcessHelper
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/11 15:32
 * @Annotation：认证流程步骤
 */
class Pre_Steps(private val iView: IView_Steps): ProPresenter(iView) {

    //记录当前进行中步骤的Title
    private var curtStepTitle = ""

    /**
     * 获取全部流程
     */
    fun loadUncleProcessStepAction(){
        val allStepTitles = CustomProcessHelper.getUncleStepTitleList()
        //当前进行的步骤
        val curtIndex = CustomProcessHelper.getUncleStepIndex()
        //移除所有的流程步骤
        iView.removeUncleStepAllViews()

        for (i in 0 until allStepTitles.size) {
            val stepTitle = allStepTitles[i]
            if (i < curtIndex){//已完成
                iView.addUncleStepAfterView(stepTitle)
            } else if (i == curtIndex){//正在进行
                curtStepTitle = stepTitle
            } else {//未开始
                iView.addUncleStepBeforeView(stepTitle)
            }
        }
        //设置所有步骤列表
        showUncleStepPage()
    }

    /**
     * 提交数据
     */
    fun requestSubmitProcessAction(params: JSONObject){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        doPostAction(httpUrl, params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.showUncleSubmitSuccess()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hideUncleLoading()
            }
        })
    }

    /**
     * 根据Step类型显示不同的Page
     */
    private fun showUncleStepPage(){
        //当前进行的步骤
        val curtIndex = CustomProcessHelper.getUncleStepIndex()
        val allSteps = CustomProcessHelper.getUncleAllStepList()
        if (curtIndex == allSteps.size){//所有步骤已完成
            iView.showUncleStepsCompletePage()
            return
        }

        val processData = CustomProcessHelper.getUncleProcessData()
        val eventTitle = allSteps[curtIndex]
        val allStepTitles = CustomProcessHelper.getUncleStepTitleList()
        val stepTitle = allStepTitles[curtIndex]
        //获取对应的key
        val curtKey = allSteps[curtIndex]
        //Contact 类型
        if (processData!!.has(curtKey)){
            val stepData = processData.optJSONObject(curtKey)
            stepData?.put("showTitle",stepTitle)
            stepData?.put("stepTitle",eventTitle)
            if (stepData != null && stepData.has("isContact") && stepData.optString("isContact") == "YES"){
                iView.showUncleStepContactPage(stepData)
                return
            }
        }

        //todo Ocr证件照样式
        if (curtKey.contains("ocr_info")){
            val stepData = JSONObject()
            stepData.put("showTitle",stepTitle)
            stepData.put("stepTitle",eventTitle)
            iView.showUncleStepOcrPage(stepData)
            return
        }

        //todo Selfie自拍照样式
        if (curtKey.contains("sefie_info")){
            val stepData = JSONObject()
            stepData.put("showTitle",stepTitle)
            stepData.put("stepTitle",eventTitle)
            iView.showUncleStepSelfiePage(stepData)
            return
        }

        //todo Account账户样式（法语地区绑定账号）
        if (curtKey.contains("account_info") && UncleConfigs.isFrench()){
            val stepData = JSONObject()
            stepData.put("showTitle",stepTitle)
            stepData.put("stepTitle",eventTitle)
            iView.showUncleStepAccountPage(stepData)
            return
        }
        //常规通用样式
        if (processData.has(curtKey)){
            val stepData = processData.optJSONObject(curtKey)
            if (stepData != null) {
                stepData.put("showTitle",stepTitle)
                stepData.put("stepTitle",eventTitle)
                iView.showUncleStepCommonPage(stepData)
            }
        }
    }

    /**
     * 获取当前步骤的title
     */
    fun getUncleCurrentStepTitle(): String{
        return curtStepTitle
    }

}